// 生成器是 ECMAScript 6 新增的一个极为灵活的结构，拥有在一个函数块内暂停和恢复代码执行的
// 能力。这种新能力具有深远的影响，比如，使用生成器可以自定义迭代器和实现协程
// 箭头函数不能用来定义生成器函数
import React from 'react'

function* genFn() {
    yield;

}
let genOb = genFn()
console.log(genOb.next())
console.log(genOb.next())
function getdata1() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('data1')
        }, 1000)
    })
}
function getdata2() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('data2')
        }, 1000)
    })
}
function getdata3() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('data3')
        }, 1000)
    })
}
function* generatorFun() {
    const a1 = yield getdata1();
    console.log(a1)
    const a2 = yield getdata2();
    console.log(a2)
    const a3 = yield getdata3();
    console.log(a3)
}
// let generatorFunObj = generatorFun()
// console.log(getdata1())
// console.log(generatorFunObj.next())
// generatorFunObj.next()
// generatorFunObj.next()

function run(gen) {
    let genObj = gen()

    function next(data) {

        let res = genObj.next(data)

        if (res.done) {
            return res.value
        }

        res.value.then(res => next(res))


    }
    next()
}
run(generatorFun)
export function App() {
    return (
        <div>App</div>
    )
}
